package com.dao.impl;

import com.dao.AccountDao;
import com.domain.Account01;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.util.List;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

    @Override
    public Account01 findAccountByid(Integer id) {
        List<Account01>  account01s=super.getJdbcTemplate().query("select * from account01 where id=?",
                new BeanPropertyRowMapper<Account01>(Account01.class),id);
        return account01s.isEmpty()?null : account01s.get(0);
    }

    @Override
    public Account01 findAccountByName(String name) {
        try{
            System.out.println(super.getJdbcTemplate().getDataSource().getConnection().getSchema());
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }
        List<Account01>  accounts= super.getJdbcTemplate().query("select * from account01 where name = ? ", new String[]{name},
                new BeanPropertyRowMapper<Account01>(Account01.class));

        return accounts.isEmpty()?null : accounts.get(0);
    }

    @Override
    public void updateAccount(Account01 account01) {
        super.getJdbcTemplate().update("update  account01 set name=?,money=? where id=?"
                ,account01.getName(),account01.getMoney(),account01.getId());
    }
}
